package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConst;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDao setmealDao;

    @Autowired
    JedisPool jedisPool;

    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //1. 添加套餐（回显主键）
        setmealDao.add(setmeal);
        //2. 维护套餐和检查组的关系
        setRelation(setmeal, checkgroupIds);
        //3. 添加成功后，在redis中移除 图片名称
        jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES, setmeal.getImg());
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> pageSetmeal = setmealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(pageSetmeal.getTotal(), pageSetmeal.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        Jedis poolResource = jedisPool.getResource();
        List<String> JsonString = poolResource.lrange(RedisConst.SETMEAL_LIST, 0, -1);
        log.debug("套餐数据！！！！！！！！！！！！！" + JsonString);
        if (JsonString.size() != 0) {
            ArrayList<Setmeal> setmealArrayList = new ArrayList<>();
            for (String SetmealJson : JsonString) {
                Setmeal setmeal = JSONObject.parseObject(SetmealJson, Setmeal.class);
                setmealArrayList.add(setmeal);
            }
            return setmealArrayList;
        } else {
            List<Setmeal> setmealList = setmealDao.findAll();
            for (Setmeal setmeal : setmealList) {
                String jsonString = JSON.toJSONString(setmeal);
                Jedis resource = jedisPool.getResource();
                resource.rpush(RedisConst.SETMEAL_LIST, jsonString);
            }
            return setmealList;
        }
    }

    @Override
    public Setmeal findDetailById(Integer id) {
        String JsonString = jedisPool.getResource().get(RedisConst.SETMEALDETAILS + id);
        if (JsonString != null) {
            Setmeal setmeal1 = JSONObject.parseObject(JsonString, Setmeal.class);
            return setmeal1;
        } else {
            Setmeal setmeal = setmealDao.findDetailById(id);
            String jsonString = JSON.toJSONString(setmeal);
            jedisPool.getResource().set(RedisConst.SETMEALDETAILS + id, jsonString);
            return setmeal;
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return setmealDao.getSetmealReport();
    }

    /**
     * 设置检查组和套餐的关系
     *
     * @param setmeal
     * @param checkgroupIds
     */
    private void setRelation(Setmeal setmeal, Integer[] checkgroupIds) {
        if (setmeal.getId() != null && checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setRelation(setmeal.getId(), checkgroupId);
            }
        }
    }
}
